package com.wdl.modules.myhouse.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wdl.modules.myhouse.common.Code;
import com.wdl.modules.myhouse.common.Result;
import com.wdl.modules.myhouse.dto.AnimeImgDTO;
import com.wdl.modules.myhouse.dto.GamesImgDTO;
import com.wdl.modules.myhouse.entity.AnimeImgEntity;
import com.wdl.modules.myhouse.entity.GamesImgEntity;
import com.wdl.modules.myhouse.service.AnimeImgService;
import com.wdl.modules.myhouse.vo.GamesImgVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * 动漫截图
 *
 * @author lucain sunlightcs@gmail.com
 * @since 1.0.0 2022-09-10
 */
@RestController
@RequestMapping("/animeImg")
@Api(tags = "动漫截图")
@Slf4j
public class AnimeImgController {
    @Autowired
    private AnimeImgService animeImgService;

    /**
     * 保存提交的图像信息
     * @param vo 此处使用GamesImgVO 是因为前台组件统一处理类型
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody GamesImgVo vo) {
        log.info("vo:{}", vo);
        AnimeImgEntity entity = new AnimeImgEntity();
        BeanUtils.copyProperties(vo, entity);
        entity.setAnimeId(vo.getId());
        entity.setId(null);
        boolean save = animeImgService.save(entity);
        if (save) {
            return new Result<>().ok();
        } else {
            return new Result<>().error(Code.COMMON_ERROR.getCode(), "保存失败");
        }
    }


    @GetMapping("/allPic/{animeId}")
    public Result<List<AnimeImgDTO>> getPicByGameId(@PathVariable("animeId") String animeId) {
        QueryWrapper<AnimeImgEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AnimeImgEntity::getAnimeId, animeId);
        List<AnimeImgEntity> entitys = animeImgService.list(wrapper);
        List<AnimeImgDTO> list = new ArrayList();
        if (entitys != null) {
            entitys.forEach(entity -> {
                AnimeImgDTO dto = new AnimeImgDTO();
                BeanUtils.copyProperties(entity, dto);
                list.add(dto);
            });
        }
        return new Result<List<AnimeImgDTO>>().ok().setData(list);
    }


}